package com.ruoyi.labdanger.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.labdanger.domain.LabApplyPurchaseDetails;
import com.ruoyi.labdanger.mapper.LabApplyPurchaseDetailsMapper;
import com.ruoyi.labdanger.service.ILabApplyPurchaseDetailsService;
import com.ruoyi.labdata.domain.LabFile;
import com.ruoyi.labdata.domain.LabFlowInstance;
import com.ruoyi.labdata.domain.LabFlowTaskLog;
import com.ruoyi.labdata.service.ILabFileService;
import com.ruoyi.labdata.service.ILabFlowInstanceService;
import com.ruoyi.labdata.service.ILabFlowTaskLogService;
import com.ruoyi.system.utils.FileTypeEnum;

/**
 * 在用化学品Service业务层处理
 * 
 * @author winner pan
 * @date 2022-03-04
 */
@Service
public class LabApplyPurchaseDetailsServiceImpl implements ILabApplyPurchaseDetailsService {
	@Autowired
	private LabApplyPurchaseDetailsMapper labApplyPurchaseDetailsMapper;
	@Autowired
	private ILabFileService labFileService;
	@Autowired
	private ILabFlowTaskLogService labFlowTaskLogService;
	@Autowired
	private ILabFlowInstanceService labFlowInstanceService;
	private static final String contractBelongType = FileTypeEnum.Contract.getValue();
	private static final String inoviceBelongType = FileTypeEnum.Inovice.getValue();
	private static final String reimburBelongType = FileTypeEnum.Reimbur.getValue();

	/**
	 * 查询在用化学品
	 * 
	 * @param purchaseDetailId
	 *            在用化学品ID
	 * @return 在用化学品
	 */
	@Override
	public LabApplyPurchaseDetails selectLabApplyPurchaseDetailsById(Long purchaseDetailId) {
		return labApplyPurchaseDetailsMapper.selectLabApplyPurchaseDetailsById(purchaseDetailId);
	}

	/**
	 * 查询在用化学品列表
	 * 
	 * @param labApplyPurchaseDetails
	 *            在用化学品
	 * @return 在用化学品
	 */
	@Override
	public List<LabApplyPurchaseDetails> selectLabApplyPurchaseDetailsList(
			LabApplyPurchaseDetails labApplyPurchaseDetails) {
		return labApplyPurchaseDetailsMapper.selectLabApplyPurchaseDetailsList(labApplyPurchaseDetails);
	}

	/**
	 * 新增在用化学品
	 * 
	 * @param labApplyPurchaseDetails
	 *            在用化学品
	 * @return 结果
	 */
	@Override
	public int insertLabApplyPurchaseDetails(LabApplyPurchaseDetails labApplyPurchaseDetails) {
		return labApplyPurchaseDetailsMapper.insertLabApplyPurchaseDetails(labApplyPurchaseDetails);
	}

	/**
	 * 修改在用化学品
	 * 
	 * @param labApplyPurchaseDetails
	 *            在用化学品
	 * @return 结果
	 */
	@Override
	public int updateLabApplyPurchaseDetails(LabApplyPurchaseDetails labApplyPurchaseDetails) {
		return labApplyPurchaseDetailsMapper.updateLabApplyPurchaseDetails(labApplyPurchaseDetails);
	}

	/**
	 * 批量删除在用化学品
	 * 
	 * @param purchaseDetailIds
	 *            需要删除的在用化学品ID
	 * @return 结果
	 */
	@Override
	public int deleteLabApplyPurchaseDetailsByIds(Long[] purchaseDetailIds) {
		return labApplyPurchaseDetailsMapper.deleteLabApplyPurchaseDetailsByIds(purchaseDetailIds);
	}

	/**
	 * 删除在用化学品信息
	 * 
	 * @param purchaseDetailId
	 *            在用化学品ID
	 * @return 结果
	 */
	@Override
	public int deleteLabApplyPurchaseDetailsById(Long purchaseDetailId) {
		return labApplyPurchaseDetailsMapper.deleteLabApplyPurchaseDetailsById(purchaseDetailId);
	}

	@Override
	public int updateDetailsQuantityById(LabApplyPurchaseDetails labApplyPurchaseDetails) {
		return this.labApplyPurchaseDetailsMapper.updateDetailsQuantityById(labApplyPurchaseDetails);
	}

	public int updateApproval(Long instanceId) {
		LabFlowInstance flowInstance = labFlowInstanceService.selectLabFlowInstanceById(instanceId);
		if(flowInstance.getFlowStatusOpt().equals("finished")){
			this.labApplyPurchaseDetailsMapper.updateApproval(instanceId);
		}
		return 1;
	}

	public int scanoutIventory(LabApplyPurchaseDetails labApplyPurchaseDetails) {
		return labApplyPurchaseDetailsMapper.scanoutIventory(labApplyPurchaseDetails);
	}

	public int scaninIventory(LabApplyPurchaseDetails labApplyPurchaseDetails) {
		return labApplyPurchaseDetailsMapper.scaninIventory(labApplyPurchaseDetails);
	}

	@Transactional
	public int updateScanIn(LabApplyPurchaseDetails labApplyPurchaseDetails) {
		labFileService.updateFiles(labApplyPurchaseDetails.getContractFiles(),
				labApplyPurchaseDetails.getPurchaseDetailId(), contractBelongType);
		labFileService.updateFiles(labApplyPurchaseDetails.getInoviceFiles(),
				labApplyPurchaseDetails.getPurchaseDetailId(), inoviceBelongType);
		labFileService.updateFiles(labApplyPurchaseDetails.getReimbursementFiles(),
				labApplyPurchaseDetails.getPurchaseDetailId(), reimburBelongType);
		LabFlowInstance instance = labFlowInstanceService
				.selectLabFlowInstanceById(labApplyPurchaseDetails.getInstanceId());
		labApplyPurchaseDetails.setOrgId(instance.getCreateOrgId());
		/*LabApplyPurchaseDetails queryDetails = labApplyPurchaseDetailsMapper.selectLabApplyPurchaseDetailsById(labApplyPurchaseDetails.getPurchaseDetailId());
		labApplyPurchaseDetails.setInventoryNum(Long.valueOf(queryDetails.getQuantity()));*/
		return this.labApplyPurchaseDetailsMapper.updateScanIn(labApplyPurchaseDetails);
	}

	public LabApplyPurchaseDetails getScanInInfo(Long purchaseDetailId) {
		LabApplyPurchaseDetails details = this.selectLabApplyPurchaseDetailsById(purchaseDetailId);
		List<LabFile> contractFiles = labFileService.getFiles(purchaseDetailId, contractBelongType);
		List<LabFile> inoviceFiles = labFileService.getFiles(purchaseDetailId, inoviceBelongType);
		List<LabFile> reimburFiles = labFileService.getFiles(purchaseDetailId, reimburBelongType);
		details.setContractFiles(contractFiles);
		details.setInoviceFiles(inoviceFiles);
		details.setReimbursementFiles(reimburFiles);
		// return
		// this.labApplyPurchaseDetailsMapper.updateScanIn(labApplyPurchaseDetails);
		return details;
	}

	public int updatePuchaseId(LabApplyPurchaseDetails labApplyPurchaseDetails) {
		return this.labApplyPurchaseDetailsMapper.updatePuchaseId(labApplyPurchaseDetails);
	}
	
	public int updateRemoved(LabApplyPurchaseDetails labApplyPurchaseDetails){
		return this.labApplyPurchaseDetailsMapper.updateRemoved(labApplyPurchaseDetails);
	}
	@Transactional
	public int updateQuantity(LabApplyPurchaseDetails labApplyPurchaseDetails, LabFlowTaskLog labFlowTaskLog){
		labFlowTaskLogService.insertLabFlowTaskLog(labFlowTaskLog);
		return this.labApplyPurchaseDetailsMapper.updateQuantity(labApplyPurchaseDetails);
	}
	
	public List<LabApplyPurchaseDetails> queryInventory(LabApplyPurchaseDetails labApplyPurchaseDetails){
		return this.labApplyPurchaseDetailsMapper.queryInventory(labApplyPurchaseDetails);
	}
	public List<LabApplyPurchaseDetails>  queryScaninData(LabApplyPurchaseDetails labApplyPurchaseDetails){
		return this.labApplyPurchaseDetailsMapper.queryScaninData(labApplyPurchaseDetails);
	}
}
